﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sispe.Entities;
using System.Data;

namespace Sispe.Repository
{
    public class PermisoRepository:MasterRepository, IPermisoRepository
    {
        public List<PERMISO> GetAllFromPermiso()
        {
            var query = from s in context.PERMISOS
                        .Include("TIPO_PERMISO")
                        select s;
            return query.ToList();
        }

        public List<PERMISO> HistorialPermisosTrabajador(Int32 idtrabajador)
        {
            var query = from s in context.PERMISOS.Include("TIPO_PERMISO")
                        where s.CONTRATO.IdTrabajador.Equals(idtrabajador)
                        select s;
            return query.ToList();
        }

        public void AddPermiso(PERMISO permiso)
        {
            permiso.TIPO_PERMISO = null;
            permiso.CONTRATO = null;

            context.PERMISOS.Add(permiso);
            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {

                var outputLines = new List<string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                        DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format(
                            "- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage));
                    }
                }
                if (System.IO.File.Exists(@"D:\errors.txt"))
                {
                    System.IO.File.Delete(@"D:\errors.txt");
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                else
                {
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                throw;
            }
        }

        public void UpdatePermiso(PERMISO permiso)
        {
            permiso.TIPO_PERMISO = null;
            permiso.CONTRATO = null;

            context.Entry(permiso).State = System.Data.EntityState.Modified;
            try
            {
                context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException e)
            {

                var outputLines = new List<string>();
                foreach (var eve in e.EntityValidationErrors)
                {
                    outputLines.Add(string.Format(
                        "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
                        DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
                    foreach (var ve in eve.ValidationErrors)
                    {
                        outputLines.Add(string.Format(
                            "- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage));
                    }
                }
                if (System.IO.File.Exists(@"D:\errors.txt"))
                {
                    System.IO.File.Delete(@"D:\errors.txt");
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                else
                {
                    System.IO.File.AppendAllLines(@"d:\errors.txt", outputLines);
                }
                throw;
            }
        }


        public List<PERMISO> GetFromPermisoByIdContrato(int contrato)
        {
            var query = from e in context.PERMISOS.Include("TIPO_PERMISO")
                        where e.IdContrato.Equals(contrato)
                        select e;
            return query.ToList();
        }
    }
}
